package projects

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"github.com/beego/beego/v2/core/logs"
	beego "github.com/beego/beego/v2/server/web"
	"github.com/jinzhu/copier"
	"strconv"
	"time"
	"xpms/app/auth/users"
	"xpms/app/dict"
	"xpms/app/models"
	"xpms/cores"
	"xpms/cores/utils"
)

type ProjectsTest struct {
	Id                    int64 `orm:"pk;column(testid);" json:"Id,string"`
	Taskid                int64 `json:"Taskid,string"`
	Needsid               int64 `json:"Needsid,string"`
	Projectid             int64 `json:"Projectid,string"`
	Userid                int64 `json:"Userid,string"`
	Acceptid              int64 `json:"Acceptid,string"`
	Ccid                  string
	Completeid            int64 `json:"Completeid,string"`
	Type                  int
	Name                  string
	Desc                  string
	Level                 int
	DirectCause           string
	RootCause             string
	RootCauseType         int
	CorrespondingStrategy string
	EvasiveStrategy       string
	Attachment            string
	Completed             int64
	Os                    string
	Browser               string
	Created               int64
	Changed               int64
	Status                int
	Projectmoduleid       int64 `json:"Projectmoduleid,string"`
	Env                   int
	Estimatedtasktime     int
	Actualtasktime        int
	Bussconfirmstatus     int
	Acceptevn             int `json:"Acceptevn,string"`
	Versionno             string
	Uatdepart             int64 `json:"Uatdepart,string"`
}

type ProjectsAllTestDto struct {
	Id                    int64 `json:"Id,string"`
	Taskid                int64 `json:"Taskid,string"`
	Needsid               int64 `json:"Needsid,string"`
	Projectid             int64 `json:"Projectid,string"`
	Projectname           string
	Userid                int64 `json:"Userid,string"`
	Userrealname          string
	Acceptid              int64 `json:"Acceptid,string"`
	Acceptrealname        string
	Ccid                  string
	Completeid            int64 `json:"Completeid,string"`
	Completerealname      string
	Type                  int
	Name                  string
	Desc                  string
	Level                 int
	DirectCause           string
	RootCause             string
	RootCauseType         int
	CorrespondingStrategy string
	EvasiveStrategy       string
	Attachment            string
	Completed             int64
	Os                    string
	Browser               string
	Created               int64
	Changed               int64
	Status                int
	Projectmoduleid       int64 `json:"Projectmoduleid,string"`
	Modulename            string
	Env                   int
	Estimatedtasktime     int
	Actualtasktime        int
	Bussconfirmstatus     int
	Acceptevn             int   `json:"Acceptevn,string"`
	Departid              int64 `json:"Departid,string"`
	Departname            string
	Versionno             string
	Uatdepart             int64 `json:"Uatdepart,string"`
	Uatdepartname         string
}

type ProjectsTestLog struct {
	Id      int64 `orm:"pk;" json:"Id,string"`
	Testid  int64 `json:"Testid,string"`
	Userid  int64 `json:"Userid,string"`
	Note    string
	Created int64
	Status  int
}

type ProjectsTestLogAll struct {
	Id           int64 `json:"Id,string"`
	Testid       int64 `json:"Testid,string"`
	Userid       int64 `json:"Userid,string"`
	Userrealname string
	Note         string
	Created      int64
	Status       int
}

func (this *ProjectsTest) TableName() string {
	return models.TableName("projects_test")
}

func (this *ProjectsTestLog) TableName() string {
	return models.TableName("projects_test_log")
}
func init() {
	orm.RegisterModel(new(ProjectsTest), new(ProjectsTestLog))
}

func AddTest(upd ProjectsTest) error {
	o := orm.NewOrm()
	pro := new(ProjectsTest)
	upd.Created = time.Now().Unix()
	upd.Changed = time.Now().Unix()
	copier.Copy(pro, upd)
	_, err := o.Insert(pro)

	if upd.Acceptid > 0 {
		email := users.GetUserEmail(upd.Acceptid)
		logs.Info("开始发送邮寄：发送邮箱" + email)
		domain, _ := beego.AppConfig.String("domain")
		link := domain + "/view/test/show/" + fmt.Sprintf("%d", upd.Id)
		content := upd.Desc + "<br/><a href=\"" + link + "\">" + link + "</a>"
		go utils.SendMail(email, "新反馈："+upd.Name, content)
	}

	//操作日志
	idpk := utils.SnowFlakeId()
	var log ProjectsTestLog
	log.Id = idpk
	log.Userid = upd.Userid
	log.Testid = upd.Id
	log.Status = upd.Status
	log.Note = users.GetRealname(upd.Userid) + "创建了反馈"
	log.Created = time.Now().Unix()
	err = AddTestLog(log)

	return err
}

func UpdateTest(id, userid int64, upd ProjectsTest) error {
	test, err := GetProjectTest(id)
	if err != nil {
		return err
	}
	var col []string
	col = append(col, "changed")
	test.Changed = time.Now().Unix()

	if !utils.IsEmpty(upd.Acceptid) {
		test.Acceptid = upd.Acceptid
		col = append(col, "acceptid")
	}
	if !utils.IsEmpty(upd.Ccid) {
		test.Ccid = upd.Ccid
		col = append(col, "ccid")
	}
	if !utils.IsEmpty(upd.Name) {
		test.Name = upd.Name
		col = append(col, "name")
	}
	if !utils.IsEmpty(upd.Desc) {
		test.Desc = upd.Desc
		col = append(col, "desc")
	}
	if !utils.IsEmpty(upd.Taskid) {
		test.Taskid = upd.Taskid
		col = append(col, "taskid")
	}
	if !utils.IsEmpty(upd.Acceptevn) {
		test.Acceptevn = upd.Acceptevn
		col = append(col, "acceptevn")
	}
	if !utils.IsEmpty(upd.Level) {
		test.Level = upd.Level
		col = append(col, "level")
	}
	if !utils.IsEmpty(upd.Type) {
		test.Type = upd.Type
		col = append(col, "type")
	}
	if !utils.IsEmpty(upd.Projectmoduleid) {
		test.Projectmoduleid = upd.Projectmoduleid
		col = append(col, "projectmoduleid")
	}
	if !utils.IsEmpty(upd.Env) {
		test.Env = upd.Env
		col = append(col, "env")
	}
	if !utils.IsEmpty(upd.Os) {
		test.Os = upd.Os
		col = append(col, "os")
	}
	if !utils.IsEmpty(upd.Status) {
		test.Status = upd.Status
		col = append(col, "status")
		//已对应才修改解决时间
		if upd.Status == 80 {
			test.Completed = time.Now().Unix()
			test.Completeid = upd.Completeid
			col = append(col, "completeid")
		} else if upd.Status == 30 {
			test.Completeid = upd.Completeid
			col = append(col, "completeid")
		}

	}
	if !utils.IsEmpty(upd.Browser) {
		test.Browser = upd.Browser
		col = append(col, "browser")
	}
	if !utils.IsEmpty(upd.Attachment) {
		test.Attachment = upd.Attachment
		col = append(col, "attachment")
	}
	//操作日志
	var log ProjectsTestLog
	log.Id = utils.SnowFlakeId()
	log.Userid = userid
	log.Testid = id
	log.Status = test.Status
	log.Note = users.GetRealname(userid) + "编辑了反馈"
	log.Created = time.Now().Unix()
	AddTestLog(log)

	if test.Acceptid > 0 || test.Userid > 0 {
		var email string
		if test.Acceptid > 0 && (test.Status == 0 || test.Status == 60) {
			email = users.GetUserEmail(test.Acceptid)
		} else if test.Userid > 0 && test.Status == 80 {
			email = users.GetUserEmail(test.Userid)
		}
		logs.Info("开始发送邮件：" + email)
		domain, _ := beego.AppConfig.String("domain")
		link := domain + "/view/test/show/" + fmt.Sprintf("%d", test.Id)
		content := test.Desc + "<br/><a href=\"" + link + "\">" + link + "</a>"
		go utils.SendMail(email, "反馈："+test.Name, content)

	}
	o := orm.NewOrm()
	_, err1 := o.Update(&test, utils.RemoveDuplicatesAndEmpty(col)...)
	return err1
}

func AddTestLog(upd ProjectsTestLog) error {
	o := orm.NewOrm()
	log := new(ProjectsTestLog)
	log.Id = upd.Id
	log.Userid = upd.Userid
	log.Testid = upd.Testid
	log.Note = upd.Note
	log.Status = upd.Status
	log.Created = time.Now().Unix()
	_, err := o.Insert(log)
	return err
}

func GetProjectTest(id int64) (ProjectsTest, error) {
	var test ProjectsTest
	var err error
	o := orm.NewOrm()

	test = ProjectsTest{Id: id}
	err = o.Read(&test)

	if err == orm.ErrNoRows {
		return test, nil
	}
	return test, err
}
func TestPageExec(condArr map[string]interface{}, page int, offset int) (ops []ProjectsAllTestDto) {
	o := orm.NewOrm()
	var test []ProjectsAllTestDto
	o.Raw(testPageSql(condArr, 1, page, offset)).QueryRows(&test)
	return test
}

func TestPageCountExec(condArr map[string]interface{}, page int, offset int) int64 {
	o := orm.NewOrm()
	var analysisAllTest int64
	o.Raw(testPageSql(condArr, 0, page, offset)).QueryRow(&analysisAllTest)
	return analysisAllTest
}

func TestExportExec(condArr map[string]interface{}) (ops []ProjectsAllTestDto) {
	o := orm.NewOrm()
	var test []ProjectsAllTestDto
	o.Raw(testPageSql(condArr, 1, 1, 100000)).QueryRows(&test)
	return test
}

/**
  组装sql
*/
func testPageSql(condArr map[string]interface{}, ifsCount, page int, offset int) (string, []interface{}) {
	var sql string
	if ifsCount == 0 {
		sql = sql + `SELECT count(1) FROM (`
	}
	sql = sql + `
	SELECT
      ppt.testid id,
	  ppt.*,
	  pp.name AS projectname,
	  ppm.name  modulename,
	  up.realname AS userrealname,
	  up1.realname AS acceptrealname,
	  up2.realname AS completerealname,
      up.departid ,
      dp.name AS departname,
      uat.name AS uatdepartname
	FROM pms_projects_test ppt
	  INNER JOIN pms_projects pp
		ON pp.projectid = ppt.projectid
	  LEFT JOIN pms_projects_module ppm
		ON ppm.moduleid = ppt.projectmoduleid
	  LEFT JOIN pms_users_profile up ON up.userid=ppt.userid
      LEFT JOIN pms_departs dp ON up.departid=dp.departid
	  LEFT JOIN pms_users_profile up1 ON up1.userid=ppt.acceptid
	  LEFT JOIN pms_users_profile up2 ON up2.userid=ppt.completeid
      LEFT JOIN pms_departs uat ON ppt.uatdepart=uat.departid
	WHERE 1 = 1 `
	param := make([]interface{}, 0, 0)
	if !utils.IsEmpty(condArr["testno"]) {
		sql = sql + ` and  ppt.testid LIKE concat('%',?,'%')`
		param = append(param, condArr["testno"])
	}
	if !utils.IsEmpty(condArr["testid"]) {
		sql = sql + ` and  ppt.testid = ?`
		param = append(param, condArr["testid"])
	}
	if !utils.IsEmpty(condArr["projectid"]) {
		sql = sql + ` and  ppt.projectid=?`
		param = append(param, condArr["projectid"])
	}
	if !utils.IsEmpty(condArr["projectmoduleid"]) {
		sql = sql + ` and  ppt.projectmoduleid in(`
		sql1, value := cores.SqlInParam(condArr["projectmoduleid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["creatId"]) {
		sql = sql + ` and  ppt.userid in(`
		sql1, value := cores.SqlInParam(condArr["creatId"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if condArr["filter"] == "unaccept" {
		sql = sql + ` and  ppt.acceptid  is null`
	} else if !utils.IsEmpty(condArr["acceptid"]) {
		sql = sql + ` and  ppt.acceptid in(`
		sql1, value := cores.SqlInParam(condArr["acceptid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["completeid"]) {
		sql = sql + ` and  ppt.completeid in(`
		sql1, value := cores.SqlInParam(condArr["completeid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["level"]) {
		sql = sql + ` and  ppt.level in(`
		sql1, value := cores.SqlInParam(condArr["level"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["acceptevn"]) {
		sql = sql + ` and  ppt.acceptevn in(`
		sql1, value := cores.SqlInParam(condArr["acceptevn"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["env"]) {
		sql = sql + ` and  ppt.env in(`
		sql1, value := cores.SqlInParam(condArr["env"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["type"]) {
		sql = sql + ` and  ppt.type in(`
		sql1, value := cores.SqlInParam(condArr["type"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["status"]) {
		sql = sql + ` and  ppt.status in(`
		sql1, value := cores.SqlInParam(condArr["status"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["uatdepart"]) {
		sql = sql + ` and  ppt.uatdepart in(`
		sql1, value := cores.SqlInParam(condArr["uatdepart"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}
	if !utils.IsEmpty(condArr["createStarted"]) {
		sql = sql + ` and  ppt.created >=?`
		param = append(param, condArr["createStarted"])
	}
	if !utils.IsEmpty(condArr["keywords"]) {
		sql = sql + ` and (ppt.name LIKE concat('%',?,'%') or ppt.testid LIKE concat('%',?,'%') or ppt.desc LIKE concat('%',?,'%')) `
		param = append(param, condArr["keywords"])
		param = append(param, condArr["keywords"])
		param = append(param, condArr["keywords"])
	}
	if !utils.IsEmpty(condArr["vesionno"]) {
		sql = sql + ` and ppt.versionno LIKE concat('%',?,'%') `
		param = append(param, condArr["vesionno"])
	}

	if !utils.IsEmpty(condArr["createEnd"]) {
		sql = sql + ` and  ppt.created <=?`
		param = append(param, condArr["createEnd"])
	}
	if !utils.IsEmpty(condArr["bussconfirmstatus"]) {
		sql = sql + ` and  ppt.bussconfirmstatus in(`
		sql1, value := cores.SqlInParam(condArr["bussconfirmstatus"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}

	if !utils.IsEmpty(condArr["estimatedtasktimestart"]) {
		sql = sql + ` and  ppt.estimatedtasktime >=?`
		param = append(param, condArr["estimatedtasktimestart"])
	}
	if !utils.IsEmpty(condArr["estimatedtasktimestartend"]) {
		sql = sql + ` and  ppt.estimatedtasktime <=?`
		param = append(param, condArr["estimatedtasktimestart"])
	}
	if !utils.IsEmpty(condArr["departid"]) {
		sql = sql + ` and  up.departid in(`
		sql1, value := cores.SqlInParam(condArr["departid"])
		sql = sql + sql1 + `)`
		param = append(param, value...)
	}

	if ifsCount == 0 {
		sql = sql + ` ) C`
	} else if ifsCount > 0 {
		if page < 1 {
			page = 1
		}
		if offset < 1 {
			offset, _ = beego.AppConfig.Int("pageoffset")
		}
		start := (page - 1) * offset
		startStr := strconv.Itoa(start)
		offsetStr := strconv.Itoa(offset)
		sql = sql + ` order by created desc LIMIT ` + startStr + `,` + offsetStr
	}
	return sql, param
}

func TestLogExec(condArr map[string]interface{}) (ops []ProjectsTestLogAll) {
	o := orm.NewOrm()
	var test []ProjectsTestLogAll
	o.Raw(testLogSql(condArr)).QueryRows(&test)
	return test
}

/**
  组装sql
*/
func testLogSql(condArr map[string]interface{}) (string, []interface{}) {
	sql := `
		SELECT
		  log.*
		FROM pms_projects_test_log log
		WHERE 1 = 1 `
	param := make([]interface{}, 0, 0)
	if !utils.IsEmpty(condArr["testid"]) {
		sql = sql + ` and  log.testid=?`
		param = append(param, condArr["testid"])
	}
	sql = sql + ` order by created desc `
	return sql, param
}

func ListProjectTestLog(testId int64) (ops []ProjectsTestLog) {
	var logs []ProjectsTestLog
	var err error
	err = utils.GetCache("ListProjectTestLog.id."+fmt.Sprintf("%d", testId), &logs)
	if err != nil {
		cache_expire, _ := beego.AppConfig.Int("cache_expire")
		o := orm.NewOrm()
		qs := o.QueryTable(models.TableName("projects_test_log"))
		cond := orm.NewCondition()
		if testId > 0 {
			cond = cond.And("testId", testId)
		}
		qs = qs.SetCond(cond)
		qs.All(&logs)
		utils.SetCache("ListProjectTestLog.id."+fmt.Sprintf("%d", testId), logs, cache_expire)
	}
	return logs
}

type ProjectsTestStatus struct {
	Id                    int64 `json:"Id,string"`
	Taskid                int64 `json:"Taskid,string"`
	Needsid               int64 `son:"Needsid,string"`
	Projectid             int64 `json:"Projectid,string"`
	Projectname           string
	Userid                int64 `json:"Userid,string"`
	Userrealname          string
	Acceptid              int64 `json:"Acceptid,string"`
	Acceptrealname        string
	Ccid                  string
	Completeid            int64 `json:"Completeid,string"`
	Completerealname      string
	Type                  int
	Name                  string
	Desc                  string
	Level                 int
	DirectCause           string
	RootCause             string
	RootCauseType         int
	CorrespondingStrategy string
	EvasiveStrategy       string
	Attachment            string
	Completed             int64
	Os                    string
	Browser               string
	Created               int64
	Changed               int64
	Status                int
	Projectmoduleid       int64 `json:"Projectmoduleid,string"`
	Modulename            string
	Env                   int
	Estimatedtasktime     int
	Actualtasktime        int
	Bussconfirmstatus     int
	Note                  string
	IfsMail               int
	Vesionno              string
	Acceptevn             int   `json:"Acceptevn,string"`
	Uatdepart             int64 `json:"Uatdepart,string"`
}

/**
  反馈状态更新逻辑
*/

func ChangeProjectTest(userid int64, u ProjectsTestStatus) error {
	o := orm.NewOrm()
	test, err := GetProjectTest(u.Id)
	var col []string
	col = append(col, "changed")
	if nil != err {
		return err
	} else {
		test.Status = u.Status
		col = append(col, "status")
		if u.Acceptid > 0 {
			test.Acceptid = u.Acceptid
			col = append(col, "acceptid")
		}
		if !utils.IsEmpty(u.Vesionno) {
			test.Versionno = u.Vesionno
			col = append(col, "versionno")
		}
		//连接测试完成 就修改解决时间
		if u.Status == 22 {
			test.Completed = time.Now().Unix()
			test.Completeid = userid
			col = append(col, "completed")
			col = append(col, "completeid")
		}
		if !utils.IsEmpty(u.EvasiveStrategy) {
			test.EvasiveStrategy = u.EvasiveStrategy
			col = append(col, "evasiveStrategy")
		}
		if !utils.IsEmpty(u.CorrespondingStrategy) {
			test.CorrespondingStrategy = u.CorrespondingStrategy
			col = append(col, "correspondingStrategy")
		}
		if !utils.IsEmpty(u.DirectCause) {
			test.DirectCause = u.DirectCause
			col = append(col, "directCause")
		}
		if !utils.IsEmpty(u.Type) {
			test.Type = u.Type
			col = append(col, "type")
		}
		if !utils.IsEmpty(u.RootCause) {
			test.RootCause = u.RootCause
			col = append(col, "rootCause")
		}
		if !utils.IsEmpty(u.Acceptevn) {
			test.Acceptevn = u.Acceptevn
			col = append(col, "acceptevn")
		}
		if !utils.IsEmpty(u.RootCauseType) {
			test.RootCauseType = u.RootCauseType
			col = append(col, "rootCauseType")
		}
		if !utils.IsEmpty(u.Actualtasktime) {
			test.Actualtasktime = u.Actualtasktime
			col = append(col, "actualtasktime")
		}
		if !utils.IsEmpty(u.Estimatedtasktime) {
			test.Estimatedtasktime = u.Estimatedtasktime
			col = append(col, "estimatedtasktime")
		}
		if !utils.IsEmpty(u.Uatdepart) {
			test.Uatdepart = u.Uatdepart
			col = append(col, "uatdepart")
		}
		test.Changed = time.Now().Unix()
		_, err = o.Update(&test, utils.RemoveDuplicatesAndEmpty(col)...)
		//操作日志
		var log ProjectsTestLog
		utils.UUID.Init(1024)
		id, _ := utils.UUID.GetId()
		log.Id = id
		log.Userid = userid
		log.Testid = u.Id
		log.Status = test.Status
		log.Note = users.GetRealname(userid) + "更改反馈状态为" + dict.GetDictByTypeAndDictId_("TEST_STATUS", strconv.Itoa(test.Status), test.Projectid).DictName + ",当前分类：" + dict.GetDictByTypeAndDictId_("TEST_TYPE", strconv.Itoa(test.Type), test.Projectid).DictName + ",当前指派人：" + users.GetRealname(test.Acceptid)
		log.Created = time.Now().Unix()
		err = AddTestLog(log)
		if u.IfsMail == 1 && (test.Acceptid > 0 || test.Userid > 0) {
			var email string
			// 32
			//42
			//82
			if test.Acceptid > 0 && (test.Status == 21 || test.Status == 10 || test.Status == 32 || test.Status == 42 || test.Status == 82) {
				email = users.GetUserEmail(test.Acceptid)
			} else if test.Userid > 0 && test.Status == 80 {
				email = users.GetUserEmail(test.Userid)
			}
			logs.Info("开始发送邮件：" + email)
			domain, _ := beego.AppConfig.String("domain")
			link := domain + "/view/test/show/" + fmt.Sprintf("%d", test.Id)
			content := test.Desc + "<br/><a href=\"" + link + "\">" + link + "</a>"
			go utils.SendMail(email, "反馈："+test.Name, content)
		}
		return err
	}
}

/**
  指派人员
*/
func ChangeProjectTestAccept(id int64, userid int64, acceptDto TestAcceptDTO) error {
	o := orm.NewOrm()
	test := ProjectsTest{Id: id}
	var col []string
	col = append(col, "changed")
	test.Changed = time.Now().Unix()
	if acceptDto.Acceptid > 0 {
		test.Acceptid = acceptDto.Acceptid
		col = append(col, "acceptid")
	}
	if acceptDto.Uatdepart > 0 && acceptDto.Type > 0 {
		test.Uatdepart = acceptDto.Uatdepart
		col = append(col, "uatdepart")
	}
	if !utils.IsEmpty(acceptDto.Note) {
		test.DirectCause = acceptDto.Note
		col = append(col, "directCause")
	}
	_, err := o.Update(&test, utils.RemoveDuplicatesAndEmpty(col)...)
	//操作日志
	var log ProjectsTestLog
	log.Id = utils.SnowFlakeId()
	log.Userid = userid
	log.Testid = id
	log.Status = test.Status
	if acceptDto.Type == 0 {
		email := users.GetUserEmail(acceptDto.Acceptid)
		logs.Info("开始发送邮件：" + email)
		domain, _ := beego.AppConfig.String("domain")
		link := domain + "/view/test/show/" + fmt.Sprintf("%d", test.Id)
		content := test.Desc + "<br/><a href=\"" + link + "\">" + link + "</a>"
		go utils.SendMail(email, "新反馈："+test.Name, content)
		log.Note = users.GetRealname(userid) + "指派给" + users.GetRealname(acceptDto.Acceptid) + "。<br/>" + acceptDto.Note
	} else {
		log.Note = users.GetRealname(userid) + "指派给" + users.GetDepartsName(acceptDto.Uatdepart) + "。<br/>" + acceptDto.Note
	}

	log.Created = time.Now().Unix()
	err = AddTestLog(log)
	return err

}

func DeleteProjectTest(id int64) error {
	o := orm.NewOrm()
	_, err := o.Delete(&ProjectsTest{Id: id})
	if err == nil {
		_, err = o.Raw("DELETE FROM "+models.TableName("projects_test_log")+" WHERE testid = ?", id).Exec()
	}
	return err
}
